/**
 * 建设工程信息网“工程信息公告”
 *  这里有很多页数据，每种只爬20页
 */
import { Base, Item } from '../base';
class gcxxgg extends Base {
    constructor() {
        super();
        this.name = '建设工程信息网(工程信息公告)';
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "lnxm";
        this.collectionShowName = "辽宁项目爬虫";
        this.keyField = "title";
    }
    getItems(pageIndex: number) {
        let result: Item[] = [];
        let flag;
        for (let i = 1; i <= 5; i++) {
            if (i == 1) {
                result = this.getData(20, i);
            }
            else if (i == 2) {
                flag = this.getData(20, i);
                result = result.concat(flag);
            }
            else if (i == 3) {
                flag = this.getData(20, i);
                result = result.concat(flag);
            }
            else if (i == 4) {
                flag = this.getData(20, i);
                result = result.concat(flag);
            }
            else if (i == 5) {
                flag = this.getData(20, i);
                result = result.concat(flag);
            }
        }
        console.log(result.length);
        return result;
    }
    getData(sum: any, i: any) {
        let result: Item[] = [];
        for (let m = 0; m < sum; m++) {
            let num = m + 1;
            let url = `http://www.lnzb.cn/lnzbtb/gcxx/003003/00300300${i}/MoreInfo.aspx?CategoryNum=00300300${i}`;
            let postData = {
                '__EVENTARGUMENT': num,
                '__EVENTTARGET': 'MoreInfoList1$Pager'
            };
            let content = this.spider.postSync(url, postData);
            let $ = this.cheerio.load(content);
            let that = this;
            try {
                $('#MoreInfoList1_DataGrid1 tr ').each(function (index, ele) {
                    let item = that.buildItem();
                    let title = $(ele).find('td').eq(1).find('a').attr('title').trim();
                    let url = 'http://www.lnzb.cn' + $(ele).find('td').eq(1).find('a').attr('href');
                   result.push(that.getExtraItem(url,title));
                });
            }
            catch (error) {
                console.error(error);
            }
        }
        return result;
    }
    getExtraItem(url: string,title:string) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let first=$('#_Sheet1 tr').first();
        let item= this.buildItem();

        $('#_Sheet1').find('tr').not(first).each(function(index,ele){
            let l=$(ele).find('td').length;
            if(l==4){
                (<any>item)[$(ele).find('td').eq(0).text().trim()]=$(ele).find('td').eq(1).text().trim();
                 
                (<any>item)[$(ele).find('td').eq(2).text().trim()]=$(ele).find('td').eq(3).text().trim();
            }else{
                (<any>item)[$(ele).find('td').eq(0).text().trim()]=$(ele).find('td').eq(1).text().trim();
            }
        });
        console.log(title,"该条信息加载完成...");
        (<any>item).url = url;
        item.source = this.name;
        item.title = title;
        return item;
    }
    getPageCount(): number {
        return 1;
    }
}
module.exports = new gcxxgg();